Imports BestCarings_TuyenDung.Business.Domain
Imports BestCarings_TuyenDung.Data.Persistence
Imports TDevFramework

Public Class TuyenDung_AutoAproveThread
    Private _t As New Timer()
    Private Shared _me As TuyenDung_AutoAproveThread
    Private _dotTuyenFact As DotTuyenDungFactory = ServiceProvider.getDotTuyenDungFactory()

    Public Shared isRunning As Boolean = False

    Public Shared Function getInstance() As TuyenDung_AutoAproveThread
        If _me Is Nothing Then
            _me = New TuyenDung_AutoAproveThread()
            _me._t.Interval = 3000
            _me._t.Enabled = True

        End If
        Return _me
    End Function
    
    Public Sub StartThread()
        isRunning = True
         AddHandler _t.Tick, AddressOf approve
    End Sub

    Public Sub StopThread()
        Try
            isRunning = False
            'sw.Close()
            _t.Stop()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub approve(ByVal obj As Object, ByVal ergs As EventArgs)
        Try
            Dim dnow As Date = Now()
            Dim ts As TimeSpan = dnow.Subtract(New Date(2012, 1, 9))

            If ts.Days > 2 Then ' Tu dong phe duyet sau 2 ngay
                'Danh sach dot tuyen dung cho phe duyet
                Dim listWaitDotTuyen As TDBindingList(Of DotTuyenDung) = _dotTuyenFact.FindByTrangThai(3)
                For Each dottuyen As DotTuyenDung In listWaitDotTuyen
                    dottuyen.trang_thai = 1 ' Dang trien khai

                    _dotTuyenFact.Update(dottuyen)
                Next
                'Send mail
                Dim mail As MailUtil = New MailUtil()
                For Each dottuyen As DotTuyenDung In listWaitDotTuyen
                    mail.sendMail(Nothing, Nothing, Nothing, Nothing)
                Next
            End If
        Catch ex As Exception
            MsgBox("Error: " & ex.Message)
        End Try


    End Sub



End Class
